<html>
<head><meta charset="utf-8"><title>Serialize example code generates error · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serialize.20example.20code.20generates.20error.html">Serialize example code generates error</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226266420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serialize%20example%20code%20generates%20error/near/226266420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JR Smith <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serialize.20example.20code.20generates.20error.html#226266420">(Feb 13 2021 at 20:50)</a>:</h4>
<p>It's been over a year since I've written a custom struct serializer. I'm following the docs from <a href="https://serde.rs/impl-serialize.html#serializing-a-struct">https://serde.rs/impl-serialize.html#serializing-a-struct</a>. I'm getting an unexpected type argument error on  fn serialize&lt;S&gt;(&amp;self, serializer: S) -&gt; Result&lt;S::Ok, S::Error&gt;. I get that the error is saying it didn't expext S::Error, but removing it causes even more errors. I'm only trying to write a custom serializer for a struct. Is there maybe a version mismatch between the docs and the most recent serde crate? I have serde = { version = "1.0.123", features = ["derive"] } in my Cargo.toml</p>



<a name="226267374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serialize%20example%20code%20generates%20error/near/226267374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serialize.20example.20code.20generates.20error.html#226267374">(Feb 13 2021 at 21:07)</a>:</h4>
<p>Have you also added the <code>where S: Serializer</code> bound?</p>



<a name="226267468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serialize%20example%20code%20generates%20error/near/226267468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JR Smith <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serialize.20example.20code.20generates.20error.html#226267468">(Feb 13 2021 at 21:08)</a>:</h4>
<p>I have, yeah. I cleared out my main and just put in the sample code. That compiles. This leads me to believe that there is some kind of conflict in the code.</p>



<a name="226267691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serialize%20example%20code%20generates%20error/near/226267691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JR Smith <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serialize.20example.20code.20generates.20error.html#226267691">(Feb 13 2021 at 21:13)</a>:</h4>
<p>I've now commented stuff out until it compiles. I'm going through line by line to figure out where the conflict was. I'll circle back once I figure it out.</p>



<a name="226267997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serialize%20example%20code%20generates%20error/near/226267997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JR Smith <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serialize.20example.20code.20generates.20error.html#226267997">(Feb 13 2021 at 21:19)</a>:</h4>
<p>It looks like it was due to how I was importing traits for serde? But I played with until the below and it works.<br>
use serde::{Serialize, Deserialize};<br>
use serde::ser::{Serializer, SerializeStruct};<br>
whereas before I had<br>
use serde::{Serialize, Serializer, Deserialize};</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>